﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PMS
{
    public partial class BD_UserInfoBLL
    {
        public Message LoginMethod(string username, string password)
        {
            View_BD_UserDepInfo user = BLLSession.Current.View_BD_UserDepInfoBLL.GetListBy(u => u.UserName == username && u.PassWord == password && u.UserState == "0").FirstOrDefault();
            if (user == null)
            {
                return new Message() { state = "passwordError", msg = "用户名密码错误！" };
            }
            else
            {
                UserInfo usr = new UserInfo() { DepNameSimplified = user.DepName, AccountLogIn = user.AccountLogIn, DepId = user.DepId, DepName = user.DepName, eMail = user.E_Mail, Id = user.ID, Name = user.UserName, PosId = user.PosId, PosName = user.PosName };

                HttpContext.Current.Session[OperateContext.InfoKey] = usr;
                HttpContext.Current.Session[OperateContext.PermissionKey] = Permission.GetPermByUserId(usr.Id);
                HttpContext.Current.Session[OperateContext.BtnPermissionKey] = Permission.GetBtnPermByUserId(usr.Id);
                #region 写入Cookie
                //2.1.2将用户id加密成字符串
                string strCookieValue = SecurityHelper.EncryptUserInfo(usr.Id.ToString());
                //2.1.3创建cookie
                HttpCookie cookie = new HttpCookie(OperateContext.InfoKey, strCookieValue);
                cookie.Expires = DateTime.Now.AddDays(1);
                HttpContext.Current.Response.Cookies.Add(cookie);
                #endregion

                return new Message() { state = "success", msg = "登陆成功！" };

            }
        }

    }
}